package org.dicr.naming;

import java.io.*;
import java.util.*;

import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * @author Igor A Tarasov <java@dicr.org>
 * @version 1.0
 */
public class ListContextServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private void printContext(PrintWriter out, Context con) throws NamingException {
		out.print("<UL>");
		Enumeration<Binding> list = con.listBindings("");
		while (list.hasMoreElements()) {
			Binding bind = list.nextElement();
			out.print("<LI>" + bind);
			if (bind.getObject() instanceof Context) {
				printContext(out, (Context) bind.getObject());
			}
			out.print("</LI>");
		}
		out.print("</UL>");
	}

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException {
		try {
			res.setContentType("text/html");
			PrintWriter out = res.getWriter();
			out.print("Hello:<BR>");
			Context con = (Context) new InitialContext().lookup("java:comp");
			printContext(out, con);
		} catch (Exception ex) {
			throw new ServletException(ex);
		}
	}
}
